package com.example.demo07cache.jedis;

import java.util.LinkedHashMap;
import java.util.Map;

public class LRUCache<K, V> extends LinkedHashMap<K, V> {

    private int capacity;

    public LRUCache(int capacity) {
        super(capacity, 0.75F, true);
        this.capacity = capacity;
    }

    @Override
    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
        return super.size() > capacity;
    }

    public static void main(String[] args) {
        LRUCache<Integer, String> cache = new LRUCache<>(3);
        cache.put(1, "a");
        cache.put(2, "b");
        cache.put(3, "c");
        System.out.println(cache.keySet());

        cache.put(4, "d");
        System.out.println(cache.keySet());

//        cache.put(3, "e");
//        System.out.println(cache.keySet());
//        cache.put(3, "e");
//        System.out.println(cache.keySet());
//        cache.put(3, "e");
//        System.out.println(cache.keySet());

        cache.get(3);
        System.out.println(cache.keySet());
        cache.get(2);
        System.out.println(cache.keySet());
        cache.get(4);
        System.out.println(cache.keySet());
    }
}
